home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / EGL_PointC215941872009.psc / PointCloud V1.1 / clsPoligonal.cls < prev    next >
Text File  |  2009-07-29  |  4KB  |  132 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "clsPolygonal"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '****************************************************************
  15. 'This is a very useful class to store polygon's Dots and
  16. 'check if the polygon is convex or not and if a point is inside
  17. 'the polygon or not
  18. 'By Raul Fragoso on 14-March-2002
  19. '****************************************************************
  20. Private Type POINTAPI
  21.     X As Long
  22.     Y As Long
  23. End Type
  24.  
  25. Private Const PI As Double = 3.14159265358979
  26. Private Polygon() As POINTAPI
  27.  
  28. ' Return True if the point is in the polygon.
  29. Public Function IsInPolygon(ByVal X As Single, ByVal Y As Single) As Boolean
  30.  
  31.     Dim pt          As Integer
  32.     Dim total_angle As Single
  33.  
  34.     ' Get the angle between the point and the
  35.     ' first and last Dots.
  36.     total_angle = GetAngle(Polygon(UBound(Polygon)).X, Polygon(UBound(Polygon)).Y, X, Y, Polygon(1).X, Polygon(1).Y)
  37.  
  38.     ' Add the angles from the point to each other
  39.     ' pair of Dots.
  40.     For pt = 1 To UBound(Polygon) - 1
  41.         total_angle = total_angle + GetAngle(Polygon(pt).X, Polygon(pt).Y, _
  42.                                               X, Y, Polygon(pt + 1).X, Polygon(pt + 1).Y)
  43.     Next pt
  44.  
  45.     ' The total angle should be 2 * PI or -2 * PI if
  46.     ' the point is in the polygon and close to zero
  47.     ' if the point is outside the polygon.
  48.     IsInPolygon = (Abs(total_angle) > 0.000001)
  49. End Function
  50.  
  51. ' Return the angle ABC.
  52. ' Return a value between PI and -PI.
  53. ' Note that the value is the opposite of what you might
  54. ' expect because Y coordinates increase downward.
  55. Private Function GetAngle(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single, ByVal CX As Single, ByVal CY As Single) As Single
  56.     
  57.     Dim dot_product     As Single
  58.     Dim cross_product   As Single
  59.  
  60.     dot_product = (Ax - Bx) * (CX - Bx) + (Ay - By) * (CY - By)     ' Calculate the dot product.
  61.     cross_product = (Ax - Bx) * (CY - By) - (Ay - By) * (CX - Bx)   ' Calculate the Z coordinate of the cross product.
  62.     GetAngle = ATan2(cross_product, dot_product)                    ' Calculate the angle.
  63.  
  64. End Function
  65.  
  66. ' Return the angle with tangent opp/hyp. The returned
  67. ' value is between PI and -PI.
  68. Private Function ATan2(ByVal opp As Single, ByVal adj As Single) As Single
  69.     
  70.     Dim angle As Single
  71.  
  72.     ' Get the basic angle.
  73.     If Abs(adj) < 0.0001 Then
  74.         angle = PI / 2
  75.     Else
  76.         angle = Abs(Atn(opp / adj))
  77.     End If
  78.  
  79.     ' See if we are in quadrant 2 or 3.
  80.     If adj < 0 Then
  81.         ' angle > PI/2 or angle < -PI/2.
  82.         angle = PI - angle
  83.     End If
  84.  
  85.     ' See if we are in quadrant 3 or 4.
  86.     If opp < 0 Then
  87.         angle = -angle
  88.     End If
  89.  
  90.     ' Return the result.
  91.     ATan2 = angle
  92.     
  93. End Function
  94.  
  95. Public Sub AddVertex(X As Long, Y As Long)
  96.     
  97.     ReDim Preserve Polygon(UBound(Polygon) + 1)
  98.     Polygon(UBound(Polygon)).X = X
  99.     Polygon(UBound(Polygon)).Y = Y
  100.     
  101. End Sub
  102.  
  103. Public Function GetVertexX(idx As Long) As Long
  104.     
  105.     GetVertexX = Polygon(idx).X
  106.  
  107. End Function
  108.  
  109. Public Function GetVertexY(idx As Long) As Long
  110.     
  111.     GetVertexY = Polygon(idx).Y
  112.  
  113. End Function
  114.  
  115. Public Function VertexCount() As Long
  116.  
  117.     VertexCount = UBound(Polygon)
  118.  
  119. End Function
  120.  
  121. Public Sub ClearDots()
  122.  
  123.    ReDim Polygon(0)
  124.  
  125. End Sub
  126.  
  127. Private Sub Class_Initialize()
  128.     
  129.     ReDim Polygon(0)
  130.  
  131. End Sub
  132.